import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const routes = [
  {
    path: '/auth/register',
    name: 'Register',
    component: () => import('@/views/auth/Register')
  },
  {
    path: '/auth/callback/:type',
    name: 'authLogin',
    component: () => import('@/views/auth/Login')
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/auth/Login')
  },
  {
    path: '/',
    name: 'Index',
    component: () => import('@/views/layouts/Index')
  },
  {
    path: '/hot',
    name: 'Hot',
    component: () => import('@/views/layouts/Index')
  },
  {
    path: '/explore',
    name: 'Explore',
    component: () => import('@/views/layouts/Index')
  },
  {
    path: '/search',
    name: 'Search',
    component: () => import('@/views/layouts/Index')
  },
  {
    path: '/:category/:tag?',
    name: 'Category',
    component: () => import('@/views/layouts/Index')
  }
]

const router = new Router({
  mode: 'history',
  routes
})
// 全局前置守卫
router.beforeEach((to, from, next) => {
  const auth = router.app.$options.store.state.auth
  window.scrollTo(0,0)
  if (auth && to.path.indexOf('/auth/') !== -1) {
    next('/')
  } else {
    next()
  }
})
export default router
